package oa.hleast.xswl.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import oa.hleast.common.constant.GlobalConstants;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.xswl.pojo.domain.UmsMember;
import oa.hleast.xswl.pojo.dto.UmsUserDto;
import oa.hleast.xswl.pojo.form.SendNoticeForm;
import oa.hleast.xswl.service.IUmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@Api(tags = "【系统管理】会员管理")
@RestController("AdminMemberController")
@RequestMapping("/api.admin/v1/members")
@Slf4j
public class MemberController {

    @Autowired
    private IUmsUserService iUmsUserService;

    @ApiOperation(value = "获取用户信息 -供其他服务调用", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "会员ID", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("getUserDtoById/{id}")
    public Result<UmsUserDto> getUserDtoById(
            @PathVariable Long id
    ) {
        UmsMember user = iUmsUserService.getById(id);
        UmsUserDto userDto = new UmsUserDto();
        BeanUtil.copyProperties(user, userDto);
        log.info("userDto = {}", userDto);
        return Result.success(userDto);
    }

    @ApiOperation(value = "列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", paramType = "query", dataType = "QueryModeEnum", dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "nickname", value = "会员昵称", paramType = "query", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping
    public Result list(
            String queryMode,
            Integer page,
            Integer limit,
            String nickname
    ) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        LambdaQueryWrapper<UmsMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(UmsMember::getDeleted, GlobalConstants.DELETED_VALUE);
        switch (queryModeEnum) {
            default: // PAGE
                queryWrapper.like(StrUtil.isNotBlank(nickname), UmsMember::getNickname, nickname);
                Page<UmsMember> umsUserPage = iUmsUserService.page(new Page<>(page, limit), queryWrapper);
                return Result.success(umsUserPage.getRecords(), umsUserPage.getTotal());
        }
    }

    @ApiOperation(value = "获取会员信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "会员ID", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/{id}")
    public Result getMemberById(
            @PathVariable Long id
    ) {
        UmsMember user = iUmsUserService.getById(id);
        return Result.success(user);
    }

    /**
     * 获取移动端用户信息
     */
    @ApiOperation(value = "获取移动端用户信息", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class)
    @GetMapping("/getAdminUserDtoById/{id}")
    Result<UmsUserDto> getAdminUserDtoById(@PathVariable Long id) {
        UmsMember user = iUmsUserService.getById(id);
        UmsUserDto userDto = new UmsUserDto();
        BeanUtil.copyProperties(user, userDto);
        return Result.success(userDto);
    }


    @ApiOperation(value = "修改会员", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "资源id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "member", value = "实体JSON对象", required = true, paramType = "body", dataType = "UmsMember", dataTypeClass = UmsMember.class)
    })
    @PutMapping(value = "/{id}")
    public Result update(
            @PathVariable Integer id,
            @RequestBody UmsMember user) {
        boolean status = iUmsUserService.updateById(user);
        return Result.judge(status);
    }


    @ApiOperation(value = "修改会员【部分更新】", httpMethod = "PATCH")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "member", value = "实体JSON对象", required = true, paramType = "body", dataType = "UmsMember", dataTypeClass = UmsMember.class)
    })
    @PatchMapping(value = "/{id}")
    public Result patch(@PathVariable Long id, @RequestBody UmsMember user) {
        LambdaUpdateWrapper<UmsMember> updateWrapper = new LambdaUpdateWrapper<UmsMember>().eq(UmsMember::getId, id);
        updateWrapper.set(user.getStatus() != null, UmsMember::getStatus, user.getStatus());
        updateWrapper.set(user.getNickname() != null, UmsMember::getNickname, user.getNickname());
        boolean status = iUmsUserService.update(updateWrapper);
        return Result.judge(status);
    }

    @ApiOperation(value = "删除会员【逻辑删除】", httpMethod = "DELETE")
    @ApiImplicitParam(name = "ids", value = "id集合", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class)
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        boolean status = iUmsUserService.update(new LambdaUpdateWrapper<UmsMember>()
                .in(UmsMember::getId, Arrays.asList(ids.split(",")))
                .set(UmsMember::getDeleted, GlobalConstants.DELETED_VALUE));
        return Result.judge(status);
    }


    // TODO 先放这边 后面改到commondWeb
    @ApiOperation(value = "发送短信", httpMethod = "POST")
    @PostMapping(value = "/sendNotice")
    public void sendNotice(@RequestBody SendNoticeForm form) {
        String content = form.getContent();
        String phone = form.getPhone();
        Map<String, String> params = form.getParams();

        log.info("content = {},phone = {},params  = {}", content, phone, params);
        try {
//            String taskId = SmsUtils.sendContent(content, phone, params);
            // 记录发送日志
        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO 先只管发送
    }

}
